package zzz.action;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import zzz.model.GetOrder;
import zzz.model.Order;
import zzz.service.OrderService;

@Controller
@RequestMapping("/order")
public class OrderAction extends BaseAction{

	@Autowired(required=true)
	public OrderService orderService;
	
	@RequestMapping("/getAllOrder")
	public String getAllOrder(Map<String,List<Order>> m){
		List<Order> ordersList=orderService.getAllOrder();
		System.out.println(ordersList);
		m.put("ordersList", ordersList);
		return "order/getAllOrder";
	}
	
	//订单详情
	@RequestMapping("/orderDetail")
	public String getOrderDetailById(String hoId,Map<String,Order> m){
		Order orderDetail=orderService.getOrderDetailById(hoId);
		System.out.println("Order:"+orderDetail);
		m.put("orderDetail", orderDetail);
		return "order/orderDetail";
	}
	
	@RequestMapping("/publishNewOrder")
	public String publishNewOrder(Order order){
		return "order/publishNewOrder";
	}
	
	@RequestMapping("/newOrder")
	public String newOrder(Order order){
		order.setUserId(Integer.toString(this.getLoginUserId()));
		System.out.println("userId:"+Integer.toString(this.getLoginUserId()));
		orderService.publishNewOrder(order);
		return "redirect:getAllOrder";
	}
	
	@RequestMapping("/getMyOrder")
	public String getMyOrder(Map<String,List<Order>> m){
		List<Order> ordersMyList=orderService.getMyOrdr(Integer.toString(this.getLoginUserId()));
		m.put("ordersMyList", ordersMyList);
		return "order/myOrder";
	}
	
	@RequestMapping("/delMyOrder")
	public String delOrder(String hoId){
		orderService.delOrder(hoId);
		//return "forward:"+getReferUrl();
		System.out.println("删除hoId："+hoId);
		return "redirect:getMyOrder";
	}
	
	@RequestMapping("/changeState")
	public String changeState(String hoId,int hoState,GetOrder getOrder,Map<String,String> m){
		System.out.println("抢单hoId:"+hoId);
		System.out.println("抢单hoState:"+hoState);
		String result=null;
		switch(hoState){
			case 0:
				orderService.updateOrderStatus(hoId, hoState);
				System.out.println("抢单userId:"+Integer.toString(this.getLoginUserId()));
				orderService.addOrderForGetOrder(getOrder,Integer.toString(this.getLoginUserId()));
				result="redirect:getAllOrder";
				break;
			case 1:
				orderService.updateOrderStatus1(hoId, hoState);
				m.put("msg", "该订单已完成！！！");
				m.put("herf", "getMyOrder");
				result="common/msgModal";
				break;
			case 2:
				break;
		}
		return result;
	}
	
	@RequestMapping("/myGetOrder")
	public String myGetOrder(Map<String,List<Order>> m){
		List<Order> myGetOrderList=orderService.myGetOrder(Integer.toString(this.getLoginUserId()));
		System.out.println("获取我抢过的订单的userId："+Integer.toString(this.getLoginUserId()));
		m.put("myGetOrderList", myGetOrderList);
		return "order/myHaveGetOrder";
	}
}
